package com.ky.pro.java.fx.jdbc;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @ClassName DataUtil
 * @Description 初始化数据等调试用工具
 * @Date 2021/12/4 21:40
 * @Author kevin
 **/
public class DataUtil {

    public static void initDB(Statement statement) throws SQLException {

        statement.execute("create table t_student\n" +
                "(\n" +
                "   ID                   bigint not null,\n" +
                "   NAME                 varchar(32),\n" +
                "   AGE                  smallint,\n" +
                "   GENDER               tinyint,\n" +
                "   KLASS_ID             bigint,\n" +
                "   primary key (ID)\n" +
                ");");

        statement.execute("create table t_klass\n" +
                "(\n" +
                "   ID                   bigint not null,\n" +
                "   NAME                 varchar(32),\n" +
                "   SCHOOL_ID            bigint,\n" +
                "   primary key (ID)\n" +
                ");");

        statement.execute("create table t_school\n" +
                "(\n" +
                "   ID                   bigint not null,\n" +
                "   NAME                 varchar(64),\n" +
                "   primary key (ID)\n" +
                ");");

    }

    public static void dumpQueryResult(ResultSet rs) throws SQLException {
        ResultSetMetaData rsMetaData = rs.getMetaData();
        int rowId = 1;
        System.out.println("Query Results: ");
        while (rs.next()) {
            System.out.println("row " + rowId + ": ");
            for (int i = 1; i <= rsMetaData.getColumnCount(); i++) {
                System.out.print(rsMetaData.getColumnName(i) + " = " + rs.getString(i) + ";");
            }
            System.out.println();
            rowId++;
        }
    }

}